<?php
/**
 * Created by PhpStorm.
 * User: qingyun
 * Date: 17/4/21
 * Time: 上午9:21
 */
namespace Admin\Controller;
use Common\Controller\AdminBaseController;

class ArticleController extends AdminBaseController{
    /**
     * 新闻列表
     */
    public function index(){
        $this -> display();
    }
    public function pageList(){
        $page = I('post.page/d',1);
        $title = I('post.title/s','');

        $whereSql = empty($title) ? '' : 'title like "%' . $title . '%" or keyword like "%' . $title . '%"';

        $article = M('article');

        // 查询满足要求的总记录数
        $count = $article->where($whereSql) -> count();

        $limit = 8;
        $pages = ceil($count / $limit);
        $start = empty($page - 1) ? 0 : (($page - 1) * $limit);

        $list = $article  -> alias('a') -> field('a.*,c.title cate') -> join('left join dx_category c on a.cate_id = c.id') -> order('a.id') -> limit($start.','.$limit) -> select();

        //  传给laypage分页js 的列表内容
        $content = '';
        foreach($list as $value){
            $content .= '<tr><td>' . $value['id'] . '</td><td>' . $value['title'] . '</td><td>';
            if(empty($value['images'])){
                $content .= '<img src="/Public/Admin/img/profile_small.jpg" style="width: 30px; height: 30px;">';
            }else{
                $content .= '<img src="/' . $value['images'] . '" style="width: 50px; height: 50px;">';
            }
            $content .= '</td><td>' . $value['cate'] . '</td><td>' . $value['tag'] . '</td><td>' . $value['author'] . '</td><td>' . $value['hits'] . '</td><td>' . mb_substr($value['content'],0,9,'utf-8') . '...</td><td>' . $value['seo_title'] . '</td><td>' . $value['seo_keyword'] . '</td><td>' . $value['seo_description'] . '</td><td>' . $value['is_html'] . '</td><td>' . $value['px'] . '</td><td><div class="col-md-12"><a class="btn btn-sm btn-outline btn-warning" href="' . U('Admin/Article/edit/id/' .$value['id']) . '"><i class="fa fa-edit"></i> 修改</a><a class="btn btn-sm btn-outline btn-danger" href="javascript:del(' . $value['id'] . ')"><i class="fa fa-trash-o"></i> 删除</a></div></td></tr>';
        }
        $data = [
            'pages' => $pages,
            'content' => $content,
        ];
        echo json_encode($data);
    }

    /**
     * 新闻添加
     */
    public function add(){
        $category = M('Category') -> where(['type' => 1, 'status' => 1]) -> field('id,title') -> select();
        $this -> assign('category', $category);
        $this -> display();
    }
    /**
     * 新闻修改
     */
    public function edit(){
        $id = I('get.id/d',0);
        $articleInfo = M('article') -> where(['id' => $id]) -> find();

        $category = M('Category') -> where(['type' => 1, 'status' => 1]) -> field('id,title') -> select();

        $this -> assign('category', $category);
        $this -> assign('articleInfo',$articleInfo);
        $this -> display('Article_add');
    }

    /**
     * 添加修改保存
     */
    public function Save(){
        if(IS_POST && IS_AJAX){
            $id = I('post.id/d',0);

            $title = I('post.title/s','');
            $cate_id = I('post.cate_id/d',0);
            $tag = I('post.tag/s','');
            $author = I('post.author/s','');
            $content = I('post.content/s','');
            $seo_title = I('post.seo_title/s','');
            $seo_keyword = I('post.seo_keyword/s','');
            $seo_description = I('post.seo_description/s','');
            $is_html = I('post.is_html/d',1);
            $px = I('post.px/d', 1);

            if(!$title){
                $this -> ajaxReturn(['status' => 1, 'msg' => '文章标题不能为空']);
            }
            if(!$tag){
                $this -> ajaxReturn(['status' => 1, 'msg' => '文章关键词不能为空']);
            }
            if(!$author){
                $this -> ajaxReturn(['status' => 1, 'msg' => '文章作者不能为空']);
            }
            if(!$content){
                $this -> ajaxReturn(['status' => 1, 'msg' => '文章内容不能为空']);
            }
            if(!$seo_title){
                $this -> ajaxReturn(['status' => 1, 'msg' => '文章SEO标题不能为空']);
            }
            if(!$seo_keyword){
                $this -> ajaxReturn(['status' => 1, 'msg' => '文章SEO关键词不能为空']);
            }
            if(!$seo_description){
                $this -> ajaxReturn(['status' => 1, 'msg' => '文章SEO描述不能为空']);
            }
            if(!$px){
                $this -> ajaxReturn(['status' => 1, 'msg' => '文章排序不能为空']);
            }
            //  获取图片
            $images = '';
            if(0 != $_FILES['images']['size']){
                $images = uploadFile($_FILES['images'], 'Upload/article/');
            }
            if(is_array($images)){
                $this -> ajaxReturn($images);
            }

            //  获取添加管理员id
            $mid = session('user.uid');

            if(!$id){
                /**
                 * ID为空 添加
                 */
                $data = [
                    'title' => $title,
                    'cate_id' => $cate_id,
                    'images' => $images,
                    'tag' => $tag,
                    'author' => $author,
                    'content' => $content,
                    'seo_title' => $seo_title,
                    'seo_keyword' => $seo_keyword,
                    'seo_description' => $seo_description,
                    'is_html' => $is_html,
                    'px' => $px,
                    'mid' => $mid
                ];

                $article = M('article');
                $isOk = $article -> add($data);
            }else{
                /**
                 * ID不为空  修改
                 */
                $data = [
                    'title' => $title,
                    'cate_id' => $cate_id,
                    'images' => $images,
                    'tag' => $tag,
                    'author' => $author,
                    'content' => $content,
                    'seo_title' => $seo_title,
                    'seo_keyword' => $seo_keyword,
                    'seo_description' => $seo_description,
                    'is_html' => $is_html,
                    'px' => $px,
                ];

                $article = M('article');
                $isOk = $article -> where(['id' => $id]) -> setField($data);
            }

            if(!$isOk){
                $this -> ajaxReturn(['status' => 1, 'msg' => '操作失败']);
            }
            $this -> ajaxReturn(['status' => 0, 'msg' => '操作成功']);
        }else{
            $this -> ajaxReturn(['status' => 1, 'msg' => '非法操作']);
        }
    }



    /**
     * 删除新闻
     */
    public function del(){
        if(IS_AJAX && IS_POST){
            $id = I('post.id/d');
            if(empty($id)){
                $this -> ajaxReturn(['status' => 1, 'msg' => '非法操作']);
            }
            $article = M('article');

            $isDel = $article -> delete($id);

            if(!$isDel){
                $this -> ajaxReturn(['status' => 1, 'msg' => '删除失败']);
            }
            $this -> ajaxReturn(['status' => 0, 'msg' => '删除成功']);
        }else{
            if(empty($id)){
                $this -> ajaxReturn(['status' => 1, 'msg' => '非法操作']);
            }
        }
    }
}